package click;

import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FindProxy implements Runnable {

    private int index;

    FindProxy(int index) {
        super();
        this.index = index;
    }

    @Override
    public void run() {
            // 测试代理ip和port有效就开始访问所有博客链接10次。
            if (CM.testIpEffective("127.0.0.1", 8888)) {
                System.out.println("本地访问开始");
//                CM.writeToFile(FakeClike.ipAndPortList.get(i).toString(), "click/aliveIP.txt");

                try {
                    visitBlogLink("127.0.0.1", 8888);
                } catch (ParserException | IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("结束，等待5分钟");

            } else {
                System.out.println("ip无效");
            }


        }


    private int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    /**
     * 访问博客,解析博客标题和访问次数
     *
     * @param ip
     * @param port
     */
    private void visitBlogLink(String ip, int port)
        throws ParserException, IOException, InterruptedException {
        for (Map.Entry<String, String> entry : FakeClike.blogMap.entrySet()) {
            Thread.sleep(10*1000);
            String url = entry.getKey();

            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
            con.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");

            con.setRequestMethod("GET");
            int responseCode = con.getResponseCode();
            String responseBody = CM.readResponseBody(con.getInputStream());
            if (responseCode == 200) {
                Parser parser;
                parser = new Parser(responseBody);
                // 获取标题标签
                NodeList titleNodes = parser.extractAllNodesThatMatch(new AndFilter(new TagNameFilter("a"), new HasAttributeFilter("href", url.replace(
                        "https://blog.csdn.net", ""))));

                if (titleNodes != null && titleNodes.size() != 0) {
                    // 转换成普通的tag标签
                    Tag tag = (Tag) titleNodes.elementAt(0);
//                    System.out.print(ip + ":" + port + "\t\t" + tag.toPlainTextString().trim() + "\t\t");
                }
                Pattern p = Pattern.compile("title=\\\"阅读次数\\\">((.*?)人阅读)<");
                Matcher m = p.matcher(responseBody);
                while (m.find()) {
                    String b = m.group(1);
                    System.out.println(b);
                }
            }
        }
    }

}
